const webpack = require('webpack');
const FriendlyErrorsWebpackPlugin = require('friendly-errors-webpack-plugin');
const path = require('path');
var TerserPlugin = require('terser-webpack-plugin');
const getVersion = require('./version/version').getVersion; // 确保这里同步获取版本号
function convertTimeZone(date, offset) {
    // 将传入的日期转换为 UTC 时间，然后添加时区偏移量
    const utc = date.getTime() + (date.getTimezoneOffset() * 60000);
    const newDate = new Date(utc + (3600000 * offset));

    return newDate.toISOString().replace('T', ' ').substring(0, 19);
}
module.exports = async function() {
    let myVersion = await getVersion(); // 获取版本号

    return {
        mode: 'production',
        entry: {
            ani: './src/root/ani.js',
            'ani.router': './src/ani_router/ani.router.miniprogram.js',
            'ani.store': './src/ani_store/ani.store.js',
            'ani.store.RD': './src/ani_store/ani.store.RD.js',
        },
        output: {
            path: path.resolve(__dirname, './dist/'+myVersion),
            publicPath: '/dist/'+myVersion+'/',
            filename: '[name].min.js',
            libraryTarget: 'commonjs2',
        },
        optimization: {
            minimize: true,
            minimizer: [
                new TerserPlugin({
                    extractComments: false,//不将注释提取到单独的文件中
                }),
            ],
        },
        plugins: [

            new FriendlyErrorsWebpackPlugin({
                compilationSuccessInfo: {
                    notes: ['版本名称：prod,当前版本号：' + myVersion, convertTimeZone(new Date(),16)],
                },
            }),
            function() {
                this.hooks.compile.tap('MyPlugin', (compilation) => {
                    console.log('\x1b[92m%s\x1b[0m', `版本【${myVersion}】编译进行中，稍安勿躁.......`);
                });
            },
            new webpack.BannerPlugin({
                banner: `/*! 当前版本：${myVersion}, 构建时间：${ convertTimeZone(new Date(),16)} */`,
                raw: true, // 如果设置为 true，将直接写入到代码中，不会被注释包裹
                entryOnly: true // 如果为 true，仅在入口 chunks 文件中添加
            }),
            new webpack.DefinePlugin({
                'VERSION': JSON.stringify(myVersion)
            }),
        ],
        target: "node",
        devtool: 'source-map',
        module: {
            rules: [
                {
                    test: /\.js$/,
                    loader: 'babel-loader',
                    exclude: /node_modules/
                }
            ]
        },
    }
};
